<template>
  <el-dialog :visible="isShow" title="分配权限" @close="close">
    <el-tree ref="tree" node-key="id" :default-checked-keys="permIds" :data="promissionList" check-strictly show-checkbox :props="defaultProps" @check-change="checkChange" />
    <template>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="mini" @click="hSave">确定</el-button>
          <el-button size="mini" @click="close">取消</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getPromissionList } from '@/api/promission'
import { transListToTreeData } from '@/utils'
import { getRoleDetail, updateRolePro } from '@/api/role'
export default {
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    roleId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      promissionList: [],
      permIds: []
    }
  },
  created() {
    this.getPromission()
  },
  methods: {
    checkChange() {
      this.permIds = this.$refs.tree.getCheckedKeys()
    },
    async hSave() {
      const res = await updateRolePro({ permIds: this.permIds, id: this.roleId })
      this.$message.success(res.message)
      this.close()
    },
    close() {
      this.$emit('update:isShow', false)
    },
    async getPromission() {
      const res = await getPromissionList()
      this.promissionList = transListToTreeData(res.data, 0)
    },
    async getRoleDetail() {
      const res = await getRoleDetail(this.roleId)
      this.permIds = res.data.permIds
    }
  }
}
</script>

<style>

</style>
